/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hellodoctor.slayer.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import com.hellodoctor.slayer.model.Appointment;
import com.hellodoctor.slayer.model.impl.AppointmentImpl;
import com.hellodoctor.slayer.service.base.AppointmentLocalServiceBaseImpl;
import com.hellodoctor.util.LConstants;
import com.liferay.portal.kernel.exception.SystemException;

/**
 * The implementation of the appointment local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.hellodoctor.slayer.service.AppointmentLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Shahbaj
 * @see com.hellodoctor.slayer.service.base.AppointmentLocalServiceBaseImpl
 * @see com.hellodoctor.slayer.service.AppointmentLocalServiceUtil
 */
public class AppointmentLocalServiceImpl extends AppointmentLocalServiceBaseImpl {
	
	public Appointment createAppointment(long doctorId,long patientId, String appointmentDate ,int appointmentTime) {
		
		
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
		Date newDate = new Date();
				
		try {
			 newDate = dateFormat.parse(appointmentDate);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Appointment appointment = new AppointmentImpl();
		long appointmentId =0l;
		try {
			 appointmentId = counterLocalService.increment(Appointment.class.getName());
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		appointment.setAppointmentId(appointmentId);
		appointment.setDoctorId(doctorId);
		appointment.setPatientId(patientId);
		appointment.setAppointmentTime(appointmentTime);
		appointment.setStatus(LConstants.Appointment_Status_Confirmed);
		appointment.setAppointmentDate(newDate);
		appointment.setCreateDate(new Date());
		
		try {
			appointment = appointmentLocalService.addAppointment(appointment);
			System.out.println("Appointmet Added from service......");
		} catch (SystemException e) {
			e.printStackTrace();
		}
		return appointment;
	}
	
	public List<Appointment> getTodaysAppointments (long doctorId){
		List<Appointment> appts = new ArrayList<Appointment>();
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
		Date newDate = new Date();
		
		String dateStr= dateFormat.format(newDate);
		try {
			newDate = dateFormat.parse(dateStr);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		try {
			appts = appointmentPersistence.findByDoctorId_ADate(doctorId, newDate);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		return appts;
		
	}
	public List<Appointment> getMyAppointments (long doctorId){
		List<Appointment> appts = new ArrayList<Appointment>();
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
		Date newDate = new Date();
		
		String dateStr= dateFormat.format(newDate);
		try {
			newDate = dateFormat.parse(dateStr);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		try {
			System.out.println("PatientId-"+doctorId);
			appts = appointmentPersistence.findByDoctorId(doctorId);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		System.out.println("My Appointmets-"+appts);
		return appts;
		
	}
	public List<Appointment> getMyAppointmentsPatient (long patientId){
		List<Appointment> appts = new ArrayList<Appointment>();
/*		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); 
		Date newDate = new Date();
		
		String dateStr= dateFormat.format(newDate);
		try {
			newDate = dateFormat.parse(dateStr);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}*/
		try {
			System.out.println("PatientId-"+patientId);
			appts = appointmentPersistence.findByPatientId(patientId);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		System.out.println("My Appointmets Patients-"+appts);
		return appts;
		
	}
	
}